/**
 *
 */
package siebes.rmi.sorter;

import java.rmi.RemoteException;
import java.util.Observable;

/**
 * @author eddie
 *
 */
public class SorterThread extends Observable implements Runnable {

    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    @SuppressWarnings("rawtypes")
    @Override
    public final void run() {
            Comparable[] result;
            try {
                result = sort(comparables);
                setChanged();
                notifyObservers(result);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    /**
     * Faciliteerd in de oplevering van sorter.
     */
    private ISortFactory sortFactory;

    /**
     * Lijst van waarden die te vergelijken zijn.
     */
    @SuppressWarnings("rawtypes")
    private Comparable[] comparables;

    /**
     * @param sortfactory faciliteerd het sorteeralgoritme.
     * @param comparable lijst met te sorteren waarden.
     * @throws RemoteException ivm RMI.
     */
    @SuppressWarnings("rawtypes")
    public SorterThread(
            final ISortFactory sortfactory,
            final Comparable[] comparable)
            throws RemoteException {
        this.sortFactory = sortfactory;
        this.comparables =  comparable;
    }

    /**
     * @param comp Ongesorteerde lijst.
     * @return Gesorteerde Lijst.
     * @throws RemoteException ivm RMI
     */
    @SuppressWarnings("rawtypes")
    private Comparable[] sort(final Comparable[] comp) throws RemoteException {
        // startThread met comparable
        ISorter sorter = sortFactory.createSorter();
        return sorter.sort(comp);
    }

    /**
     * @return het huidige nummer van de Thread.
     */
    public final int getThreadNumber() {
        return threadNumber;
    }

    /**
     * @param threadNumb in te stellen threadnummer.
     */
    public final void setThreadNumber(final int threadNumb) {
        this.threadNumber = threadNumb;
    }

    /**
     * het nummer van de huidige thread.
     */
    private int threadNumber;

}
